<template>
  <view class="seckill">
    <view class="seckill-header">
      <view class="title">
        <image src="https://m.360buyimg.com/babel/jfs/t1/112591/14/32754/2993/6358ea59E23fb3ac4/aaa6e7ed57d3d12c.png" mode="heightFix"></image>
        <text>京东秒杀</text>
      </view>
      <view class="countdown">
        <text class="tip">本场结束还剩</text>
        <view class="time-box">{{ countdown.hours }}</view>
        <text class="colon">:</text>
        <view class="time-box">{{ countdown.minutes }}</view>
        <text class="colon">:</text>
        <view class="time-box">{{ countdown.seconds }}</view>
      </view>
      <view class="more" @tap="goToSeckill">
        <text>更多秒杀</text>
        <text class="arrow">></text>
      </view>
    </view>
    <scroll-view class="seckill-goods" scroll-x show-scrollbar="false">
      <view class="goods-item" 
            v-for="(item, index) in seckillGoods" 
            :key="index" 
            @tap="goToDetail(item)">
        <view class="goods-img">
          <image :src="item.image" mode="aspectFill"></image>
          <view class="tag" v-if="item.tag">{{ item.tag }}</view>
        </view>
        <view class="goods-info">
          <text class="goods-title">{{ item.title }}</text>
          <view class="price-box">
            <view class="price">
              <text class="symbol">¥</text>
              <text class="number">{{ item.price }}</text>
            </view>
            <text class="original-price">¥{{ item.originalPrice }}</text>
          </view>
          <view class="progress-box">
            <view class="progress-bar">
              <view class="progress" :style="{ width: item.progress + '%' }"></view>
            </view>
            <text class="progress-text">已抢{{ item.progress }}%</text>
          </view>
          <view class="btn" :class="{ 'btn-disabled': item.progress === 100 }">
            {{ item.progress === 100 ? '已抢完' : '立即抢购' }}
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'

defineProps({
  seckillGoods: {
    type: Array,
    default: () => []
  }
})

const emit = defineEmits(['goToSeckill', 'goToDetail'])

const countdown = ref({
  hours: '00',
  minutes: '00',
  seconds: '00'
})

let timer = null

const updateCountdown = () => {
  const end = new Date().setHours(23, 59, 59)
  const now = new Date().getTime()
  const diff = Math.max(0, end - now)
  
  const hours = Math.floor(diff / (1000 * 60 * 60))
  const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
  const seconds = Math.floor((diff % (1000 * 60)) / 1000)
  
  countdown.value = {
    hours: hours.toString().padStart(2, '0'),
    minutes: minutes.toString().padStart(2, '0'),
    seconds: seconds.toString().padStart(2, '0')
  }
}

const goToSeckill = () => {
  emit('goToSeckill')
}

const goToDetail = (item) => {
  emit('goToDetail', item)
}

onMounted(() => {
  updateCountdown()
  timer = setInterval(updateCountdown, 1000)
})

onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
  }
})
</script>

<style scoped>
.seckill {
  margin: 20rpx;
  background: #fff;
  border-radius: 16rpx;
  padding: 20rpx;
}

.seckill-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20rpx;
}

.seckill-header .title {
  display: flex;
  align-items: center;
}

.seckill-header .title image {
  height: 40rpx;
  margin-right: 10rpx;
}

.countdown {
  display: flex;
  align-items: center;
}

.countdown .tip {
  font-size: 24rpx;
  color: #666;
  margin-right: 10rpx;
}

.countdown .time-box {
  background: #333;
  color: #fff;
  padding: 4rpx 8rpx;
  border-radius: 4rpx;
  font-size: 24rpx;
  min-width: 40rpx;
  text-align: center;
}

.countdown .colon {
  margin: 0 4rpx;
  color: #333;
  font-weight: bold;
}

.more {
  font-size: 24rpx;
  color: #666;
  display: flex;
  align-items: center;
}

.more .arrow {
  margin-left: 4rpx;
}

.seckill-goods {
  white-space: nowrap;
}

.seckill-goods .goods-item {
  display: inline-block;
  width: 280rpx;
  margin-right: 20rpx;
  background: #fff;
  border-radius: 12rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.05);
}

.goods-img {
  position: relative;
  width: 280rpx;
  height: 280rpx;
}

.goods-img image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.goods-info {
  padding: 16rpx;
}

.goods-title {
  font-size: 26rpx;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  white-space: normal;
  height: 72rpx;
  line-height: 1.4;
  margin-bottom: 12rpx;
}

.price-box {
  margin-bottom: 10rpx;
}

.price {
  padding: 10rpx;
  text-align: center;
}

.price .symbol {
  font-size: 24rpx;
  color: #ff4444;
}

.price .number {
  font-size: 32rpx;
  font-weight: bold;
  color: #ff4444;
  margin-right: 10rpx;
}

.original-price {
  font-size: 24rpx;
  color: #999;
  text-decoration: line-through;
}

.progress-box {
  margin-bottom: 10rpx;
}

.progress-bar {
  height: 30rpx;
  background: #fef0f0;
  border-radius: 15rpx;
  margin: 10rpx;
  position: relative;
  overflow: hidden;
}

.progress-bar .progress {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  background: linear-gradient(to right, #ff6e6e, #ff4444);
  border-radius: 15rpx;
  transition: width 0.3s;
}

.progress-bar .progress-text {
  position: absolute;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 20rpx;
  color: #fff;
  line-height: 30rpx;
  z-index: 1;
  text-shadow: 0 0 2rpx rgba(0,0,0,0.3);
}

.btn {
  padding: 10rpx;
  text-align: center;
  background: #ff4444;
  color: #fff;
  border-radius: 15rpx;
  font-size: 28rpx;
  cursor: pointer;
}

.btn-disabled {
  background: #999;
  cursor: not-allowed;
}
</style> 